Public Class Form1
    Dim numpedido As String = ""
    Dim destino As String = ""

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            Dim wsRf As New wsRf.Service
            Dim ds As New Data.DataSet
            Dim query As String = ""

            Try
                query += " select getnombreubicacion(h.id_empresa,h.id_ubicacion) Origen	"
                query += " 	, getnombreubicacion(c.id_cliente,c.id_ubicacion) tienda        "
                query += " 	, oc_cliente id_numero_pedido                                   "
                query += " 	, sum(cantidad) qtyGuia                                         "
                query += " from transaccion_header h                                        "
                query += " , transaccion_despac c                                           "
                query += " , transaccion_detail d                                           "
                query += " where h.id_trx = c.id_trx                                        "
                query += " 	and h.numero_documento= " + TextBox1.Text + "                   "
                query += " and h.id_trx = d.id_trx                                          "
                query += " group by h.id_empresa                                            "
                query += " 	, h.id_ubicacion                                                "
                query += " 	, c.id_cliente                                                  "
                query += " 	, c.id_ubicacion                                                "
                query += " 	, oc_cliente  									                "

                ds = wsRf.SqlEjecutaSql("", query)

                If Not IsNothing(ds) Then
                    If ds.Tables.Count > 0 Then
                        If ds.Tables(0).Rows.Count > 0 Then
                            TextBox2.Text = ds.Tables(0).Rows(0)("Origen").ToString().Trim()
                            TextBox6.Text = ds.Tables(0).Rows(0)("qtyGuia").ToString().Trim()
                            TextBox5.Text = "0"
                            numpedido = ds.Tables(0).Rows(0)("id_numero_pedido").ToString().Trim()
                            TextBox3.Focus()
                        Else
                            MessageBox.Show("No existen registros para esta Guia")
                        End If
                    Else
                        MessageBox.Show("No existen registros para esta Guia")
                    End If
                Else
                    MessageBox.Show("Error al ejecutar consulta")
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            Dim wsRf As New wsRf.Service
            Dim ds As New Data.DataSet
            Dim query As String = ""

            Dim SKU As String = ""
            Dim caja3 As String = TextBox3.Text
            Dim cantidad As Integer = caja3.Length

            SKU = caja3.Substring(0, cantidad - 1)

            Try
                query += " select tienda_destino										"
                query += "     , getnombreubicacion(86966300,tienda_destino) tienda     "
                query += "     , id_numero_pedido                                       "
                query += " from traspaso_retail_d td                                    "
                query += " where td.id_numero_pedido = " + numpedido + "                "
                query += "  and id_sku = " + SKU + "                                    "
                query += "  and cantidad_pedida - cantidad_enviada > 0                  "
                query += "  and rownum = 1                                              "

                ds = wsRf.SqlEjecutaSql("", query)

                If Not IsNothing(ds) Then
                    If ds.Tables.Count > 0 Then
                        If ds.Tables(0).Rows.Count > 0 Then
                            destino = ds.Tables(0).Rows(0)("tienda_destino").ToString().Trim()
                            TextBox4.Text = ds.Tables(0).Rows(0)("tienda").ToString().Trim()
                            Button1.Focus()
                        Else
                            MessageBox.Show("No existen datos")
                            TextBox3.Text = ""
                            TextBox3.Focus()
                        End If
                    Else
                        MessageBox.Show("No existen datos")
                        TextBox3.Text = ""
                        TextBox3.Focus()
                    End If
                Else
                    MessageBox.Show("Error al ejecutar consulta")
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            Dim wsRf As New wsRf.Service '22852.67
            Dim query As String = ""

            query += " pruenba para update traspaso_retail_d							"
            query += " set cantidad_enviada = cantidad_enviada  + 1     "
            query += " where id_numero_pedido=" + numpedido + "         "
            query += " and tienda_destino=" + destino + "               "
            query += " and id_sku = " + TextBox3.Text + "               "

            Dim IRes As Integer = wsRf.ExecuteNonQuery("", query)

            If IRes > 0 Then
                MessageBox.Show("Registro Insertado")
            Else
                MessageBox.Show("Error al insertar")

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        TextBox3.Focus()
    End Sub
End Class
